package com.zqx.strategy.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;

/**
 * xml文件解析类
 * @author zqx
 * @date 2018/09/02
 */
public class XMLUtil {

    public static Object getBean(){

        System.out.println("测试策略mac");
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document doc = documentBuilder.parse(new File("src/main/java/com/zqx/strategy/config/config.xml"));
            NodeList classNameNodeList = doc.getElementsByTagName("className");
            Node classNameNode = classNameNodeList.item(0).getFirstChild();
            String className = classNameNode.getNodeValue();

            //通过类名生成实例对象并将其返回
            Class<?> c = Class.forName(className);
            Object o = c.newInstance();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}